package lancooPaperContent;

import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperFavoritesPage.FavoritesPagePaperActivity.InforFavoritesPage;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;
import static testPaperWrongHistory.HistoryWrongQuesPaperActivity.InforHistoryWrongQues;


/**
 * Created by Administrator on 2017/3/14.
 */
public class ContentWxxzAdapter extends WrapBaseAdapter

{

    private LayoutInflater mInflater;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private ViewHolder holder = null;
    public class ViewHolder{
        TextView TvTheme;//大题文本题干1
        public ViewHolder(View convertView) {
            TvTheme = (TextView) convertView.findViewById(R.id.tv_theme);
        }
    }

    private String theme;//获取大题题干信息
    private int totalItemCount;//选择题个数


    public ContentWxxzAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom =modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_content_wxxz_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取试题相关信息
        * */
        getInfor();

        /*
        * 显示大题题干
        * */
        getContent();

        return convertView;
    }

    /*
    * 获取试题信息
    * */
    private void getInfor(){

        if(ModelFrom== PaperDef.FROM_BASE_REVIEW) {
            totalItemCount = InforBasicReview.getPaperContent().get(PaperID).getTotalItemCount();
            theme = InforBasicReview.getPaperContent().get(PaperID).getTheme();
        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            totalItemCount = InforDifficultReview.getTotalItemCount();
            theme = InforDifficultReview.getTheme();
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            totalItemCount = InforGuessTesting.getPaperContent().get(PaperID).getTotalItemCount();
            theme = InforGuessTesting.getPaperContent().get(PaperID).getTheme();
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            totalItemCount = InforWrongQues.get(PaperID).getTotalItemCount();
            theme = InforWrongQues.get(PaperID).getTheme();
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG_History||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History){
            totalItemCount = InforHistoryWrongQues.get(PaperID).getTotalItemCount();
            theme = InforHistoryWrongQues.get(PaperID).getTheme();
        }else if(ModelFrom== PaperDef.FROM_FAVOURITE_REVIEW){
            totalItemCount = InforFavoritesPage.getTotalItemCount();
            theme = InforFavoritesPage.getTheme();
        }

        String arrayTheme[] = new String[totalItemCount+1];
        String themeAll = "";
        for(int i = 0;i<totalItemCount+1;i++) {
            arrayTheme[i] = Control.returnArray(theme, "____", 4)[i];
            if(i<totalItemCount) {
                themeAll = themeAll + arrayTheme[i] + "<font color='#3399cc'><middle><middle><U>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"（"+(i+1)+"）"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
            }else{
                themeAll = themeAll + arrayTheme[i];
            }
        }
        theme = StringEscapeUtils.unescapeXml(themeAll);

    }


    /*
    * 获取题目题干信息
    * */
    Handler htmlHandlerTheme = new Handler();
    private void getContent(){

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                theme = StringEscapeUtils.unescapeXml(theme);
                final Spanned textTheme = Html.fromHtml(theme, Control.imgGetter,null);
                htmlHandlerTheme.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        holder.TvTheme.setText(textTheme);
                    }
                });

            }
        }).start();

//        holder.VvTheme.loadDataWithBaseURL("about:blank",theme,"text/html","utf-8",null);

    }

}